# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import administratorsdb
from MainPage import MainPage

class LoginPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量root
        self.root.geometry('%dx%d' % (300, 180))  # 设置窗口大小

        # 定义可以在页面更新数据的变量 普通字符串改变之后无法及时在页面中刷新
        self.username = tk.StringVar()
        self.password = tk.StringVar()

        self.page = tk.Frame(self.root)  # 创建Frame
        # 将控件布局到 root 对象 （GUI 程序对象）
        self.page.pack()
        self.create_widgets()

    def create_widgets(self):
        tk.Label(self.page).grid(row=0, stick=tk.W)
        tkinter.Label(self.page, text="账号:").grid(row=1, stick=tk.W, pady=10)
        tkinter.Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=tk.E)
        tkinter.Label(self.page, text="密码:").grid(row=2, stick=tk.W, pady=10)
        tkinter.Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=tk.E)
        tkinter.Button(self.page, text="登录", command=self.login_check).grid(row=3, stick=tk.W, pady=10)
        tk.Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=tk.E)

    def login_check(self):
        """登录检测"""
        name = self.username.get()
        secret = self.password.get()
        if administratorsdb.check_credentials(name, secret):
            self.page.destroy()
            MainPage(self.root)
        else:
            tkinter.messagebox.showinfo(title='错误', message='账号或密码错误！')


if __name__ == '__main__':

    root = tk.Tk()
    MainPage(root)
    root.mainloop()
